<%--
  Created by IntelliJ IDEA.
  User: liuxi
  Date: 2018/5/30
  Time: 17:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/bootstrap/css/bootstrap.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/table/bootstrap-table.min.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/date/bootstrap-datetimepicker.min.css"/>
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/statics/sweetalert.css"/>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/sweetalert.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/jquery-3.2.1.min.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/table/bootstrap-table.min.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/table/locale/bootstrap-table-zh-CN.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/date/Moment.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/date/bootstrap-datetimepicker.min.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/table/rawgit-bootstrap-editable.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/table/combodate.js"></script>
    <script type="text/javascript"
            src="${pageContext.request.contextPath}/statics/table/bootstrap-table-editable.min.js"></script>
</head>
</head>
<body>
<div class="panel panel-default" style="width: 50%; margin: 0 auto; margin-top:30px;">
    <div class="panel-heading">
        <h3 class="panel-title">添加病例</h3>
    </div>
    <div class="panel-body">
        <div class="form-inline" style="margin-bottom: 20px;">
            <div class="panel panel-default">
                <div class="panel-body">
                    患者信息
                </div>
                <div class="panel-footer">
                    <label for="pId">患者编号:</label>
                    <span id="pId" style="margin:0 5px;">${p.p_id}</span>
                    <label for="pName" style="margin-left: 20px; ">患者姓名:</label>
                    <span id="pName">${p.p_name}</span>
                    <br/>
                    <label for="pSex" style="margin-left: 0px; ">性别:</label>
                    <span id="pSex">${p.p_sex}</span>
                    <label for="rDate" style="margin-left:20px; ">登记时间:</label>
                    <span id="rDate">${p.rDate}</span>
                </div>
            </div>
        </div>
        <div class="form-inline">
            <label for="da">就诊项目:</label>
            <button type="button" onclick="AddItem('texter')" class="btn btn-default" id="da">使用大本框</button>
            <button type="button" onclick="AddItem('input-text')" class="btn btn-default" id="xiao">使用小本框</button>
            <div class="form-inline itemPojectInfo" id="item">
                <input type="text" class="form-control itemPoject" style="width: 200px">
                <button type="button" onclick="AddItem('add')" class="btn btn-default">添加</button>
                <span class="ItemerrInfo"></span>
                <br/>
            </div>
        </div>
        <div class="form-group">
            <label for="cCompld">主诉:</label>
            <textarea class="form-control" rows="3" id="cCompld" style="resize: none"></textarea>
        </div>
        <div class="form-group">
            <label for="cDiagnosisInfo">诊断信息:</label>
            <textarea class="form-control" rows="3" id="cDiagnosisInfo" style="resize: none"></textarea>
        </div>
        <div class="form-group">
            <label for="prescription">处方:</label>
            <button type="button" class="btn btn-default" onclick="$('#MyModal').modal();">选择药品</button>
            <textarea class="form-control" rows="3" id="prescription" style="resize: none"></textarea>
        </div>
        <div class="form-group">
            <label for="cReviewIstrue">是否需要复诊:</label>
            <input type="checkbox" id="cReviewIstrue">
        </div>
        <div class="form-group" id="a">
            <label for="nEntryDate">复查检查日期:</label>
            <div class="input-group input-daterange">
                <input id="nEntryDate" type="text" class="form-control">
                <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
            </div>
        </div>
        <div class="form-group">
            <label for="cProposal">建议:</label>
            <textarea class="form-control" rows="3" id="cProposal" style="resize: none"></textarea>
        </div>
        <button type="button" class="btn btn-default" onclick="Res()" style="margin-right:20px;">重置</button>
        <button type="buuton" class="btn btn-default" onclick="Submit()">添加</button>
    </div>
</div>
<div id="MyModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document" style="width: 1200px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">选择药品</h4>
            </div>
            <div class="modal-body">
                <div class="form-inline">
                    <label for="MName">药品名称:</label>
                    <input id="MName" type="text" class="form-control">
                    <button type="button" onclick="search()" class="btn btn-primary">搜索</button>
                </div>
                <table id="table"></table>
            </div>
            <span id="totalMoney"></span>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
                <button type="button" onclick="SubmitYao()" class="btn btn-primary">提交</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>
</body>
<script type="text/javascript">


    function Res() {
        $("#xiao").trigger("click");
        $("#cCompld").val("");
        $("#cDiagnosisInfo").val("");
        $("#prescription").val("");
        $("#cProposal").val("");
    }

    function SubmitYao() {
        var opts = $("#table").bootstrapTable("getSelections");
        var text = "";

        for (var i = 0; i < opts.length; i++) {
            if(opts[i].count !="undefined" && opts[i].count !=undefined){
                text = text + "" + (Number(i) + 1) + "." + opts[i].mName + "        数量:" + opts[i].count + "/" + "  ";
            }else{
                text = text + "" + (Number(i) + 1) + "." + opts[i].mName + "        数量:" +1 + "/" + "  ";
            }
        }
        $("#prescription").val(text);
        $("#MyModal").modal("hide");
    }

    function AddItem(id) {
        var countItem = $(".itemPoject").length;
        switch (id) {
            case "add":
                if (countItem != 3) {
                    $("<input type='text' class='form-control itemPoject' style='width: 200px';" +
                        " margin-button:20px;/><br/>").appendTo($("#item"));
                } else {
                    $(".ItemerrInfo").html("最多添加三项");
                }
                break;
            case "texter":
                $("#item").html("");
                $("<textarea class='form-control itemPoject' rows='4'style='resize: none'></textarea>").appendTo($("#item"));
                break;
            case "input-text":
                $("#item").html("");
                $(" <input type='text' class='form-control itemPoject' style='width: 200px'>" +
                    "<button type='button' onclick=\"AddItem('add')\" class='btn btn-default' >添加</button>" +
                    "<span class='ItemerrInfo'></span><br/>").appendTo($("#item"));
                break;
        }
    }

    function GetTotalMoney() {
        var totalrow = $("#table").bootstrapTable("getSelections");
        var Allmoney = 0;
        for (var i = 0; i < totalrow.length; i++) {
            Allmoney = Allmoney + (Number(totalrow[i].money) * Number(totalrow[i].count==undefined?1:totalrow[i].count));
        }
        $("#totalMoney").html(Allmoney);
    }

    function search() {
        var opts = $("#table").bootstrapTable('getOptions');
        var Mname = $("#MName").val();
        $.ajax({
            "url": "${pageContext.request.contextPath}/medicine/select",
            "type": "post",
            "data": {
                "mName": Mname,
                "pageNumber": opts.pageNumber,
                "pageSize": opts.pageSize
            },
            "dataType": "json",
            "success": function (data) {
                $("#table").bootstrapTable('load', data);
            }
        })
    }

    $(document).on("click", $("#table").find("input[type='checkbox']"), function () {
        GetTotalMoney();
    })

    /**
     * 全局变量
     */
    var row1;        //获取所有列

    $(function () {
        $.ajax({
            url: "${pageContext.request.contextPath}/",
            type: "post",
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    $("<li><a href='javaScript:'>" + data[i].ic_typeName + "</a></li>").appendTo($(".zhenInfo"));
                }
            }
        })
        // 时间插件
        $('#nEntryDate').datetimepicker({
            //viewMode: 'day',
            format: 'YYYY-MM-DD',
            minDate: (new Date())
        });
        var nid = ${nurse.nId};

        $("#cReviewIstrue").bind("change", function () {
            if ($("input[type='checkbox']").is(':checked')) {
                $("#a").css("display", "block");
            } else {
                $("#a").css("display", "none");
            }
        });
        $("#a").css("display", "none");

        $("#table").bootstrapTable({
            url: "${pageContext.request.contextPath}/medicine/select",
            onClickRow: function (row, $element) {
                row1 = row;
            },
            columns: [{
                checkbox: true
            }, {
                title: "药品编号",
                field: "medicineId",
                undefinedText: 1
            }, {
                title: "药品名称",
                field: "mName"
            }, {
                title: "药品规格",
                field: "spec"
            }, {
                title: "剩余数量",
                field: "nbumber"
            }, {
                title: "药品剂型",
                field: "dosageform",
                formatter: function (value, row, index) {
                    return "/" + row.dosageform;
                }
            }, {
                title: "药品有效期",
                field: "termofvalidity"
            }, {
                title: "单价",
                field: "money"
            }, {
                title: "拿取数量",
                field: "count",
                formatter: function (value, row, index) {
                    return "1";
                },
                editable: {
                    title: "拿取数量",
                    type: "text",
                    placement: "left",
                    validate(v) {
                        var test = /^\d*$/;
                        if (test.test(v) == false) {
                            return "请输入正确的数量";
                        }
                        if (v > row1.nbumber) {
                            return "输入数量不能大于已有数量";
                        }
                    }
                }
            }],
            clickToSelect: true,
            editable: true,
            method: 'post',
            contentType: "application/x-www-form-urlencoded",
            height: 500,
            pageList: [5, 10, 15],
            pageSize: 5,
            sortable: true,
            sortOrder: "asc",
            pageNumber: 1,
            pagination: true,
            queryParamsType: '',
            queryParams: queryParams,
            sidePagination: 'server'
        })

        function queryParams(params) {
            var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                pageSize: params.pageSize,  //页面大小
                pageNumber: params.pageNumber, //页码
                mName: $("#MName").val()
            };
            return temp;
        }
    })
    function Submit() {
        //获取用户id
        var pid = $("#pId").html();
        var item = $(".itemPoject");
        var itemStr = "";
        var EntryDate;
        // 获取就诊项目
        if (item.length > 1) {
            for (var i = 0; i < item.length; i++) {
                itemStr=itemStr+"/"+item[i].value;
            }
        }else{
            itemStr=item.value;
        }
        // 获取主诉
        var zhusu= $("#cCompld").val();
        // 获取诊断信息
        var zhenInfo=$("#cDiagnosisInfo").val();
        //获取处方
        var prescription=$("#prescription").val();
        var ischeck= $("#cReviewIstrue").prop("checked");
        if(ischeck){
            EntryDate=$("#nEntryDate").val();
            ischeck=1;
        }else{
            ischeck=0;
        }
        // 处方
        var Proposal=$("#cProposal").val();
        //医生编号
        var nid=${nurse.nId};
        //日程编号
        var sid=${p.sid};
        //预约编号
        var mid=${p.m_id};
        //总金额
        var money=$("#totalMoney").html();
        $.ajax({
            url:"${pageContext.request.contextPath}/case/AddCase",
            type:"post",
            data:{"pId":pid,"cVisitProjet":itemStr,"cCompld":zhusu,"cDiagnosisInfo":zhenInfo
                ,"cReviewIstrue":ischeck,"cReviewTime":EntryDate,"cProposal":Proposal,"nId":nid,"prescription":prescription,"sid":sid,"mid":mid,
                "money":money},
            success:function (data) {
                if(data==true){
                    alert("成功");
                }
            }
        })
    }
</script>
</html>
